// pages/tradingRecord/tradingRecord.js
var util = require('../../utils/util.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    submitload: false, //点击按钮的加载状态
    buttonclickdou: true, //点击按钮连击状态控制
    unorderlistst:false, //暂无记录显示状态 ， 默认 false 不显示
	 //搜索条件
	 orderStatusArr: ['未取件','已取件', '已送到', '派件中', '已签收'],
	 orderStatusText: '订单状态', //状态值
	 orderStatusVal: 0, //状态值
	 date: '时间',  //具体时间
	 dates: '',  //具体时间
	 //搜索条件
	 searchPageNum: 1,   // 设置加载的第几次，默认是第一次
	 tradingRecordListData: [],//交易记录列表数据
	 upslideLoading: true,  //"上拉加载"的变量，默认true，隐藏
	 upslideNoData: true,	//“没有数据”的变量，默认true，隐藏
   ifRefresh: false,	//是否是下拉刷新
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
	  app.publicchangenavigate()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
	  //请求
	  this.requestorderlist()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    wx.showNavigationBarLoading() //在标题栏中显示加载
    //初始化参数
    that.setData({
      searchPageNum: 1,   // 设置加载的第几次，默认是第一次
      upslideLoading: true,  //"上拉加载"的变量，默认true，隐藏
      upslideNoData: true,	//“没有数据”的变量，默认true，隐藏 
      ifRefresh: true,
    })
    //模拟加载
    setTimeout(function () {
      //请求
      that.requestorderlist()
    }, 1000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
	  var that = this
    if (that.data.upslideNoData && that.data.unorderlistst == false){
		  that.setData({
			  searchPageNum: that.data.searchPageNum + 1,//增加分页数
		  })
		  //请求
		  that.requestorderlist()
	  }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
	return app.globalshare();
  },
  //订单状态
  timeScreenTap:function(e){
    var that = this;
	 switch (parseInt(e.detail.value)) {
		 case 0:
			 that.setData({
				 orderStatusText: '未取件',
				 orderStatusVal: 1
			 })
			 break;
		 case 1:
			 that.setData({
				 orderStatusText: '已取件',
				 orderStatusVal: 2
			 })
			 break;
		 case 2:
			 that.setData({
				 orderStatusText: '已送到',
				 orderStatusVal: 3
			 })
			 break;
		 case 3:
			 that.setData({
				 orderStatusText: '派件中',
				 orderStatusVal: 4
			 })
			 break;
		 case 4:
			 that.setData({
				 orderStatusText: '已签收',
				 orderStatusVal: 5
			 })
			 break;
	 	}
	 that.setData({
		 searchPageNum: 1,   // 设置加载的第几次，默认是第一次
		 tradingRecordListData: [],//交易记录列表数据
		 upslideLoading: true,  //"上拉加载"的变量，默认true，隐藏
		 upslideNoData: true	//“没有数据”的变量，默认true，隐藏 
	 })
	 //重新请求
	 this.requestorderlist()
  },
  //全部订单列表
  allorderlist: function (e) {
	  this.setData({
		  orderStatusText: '订单状态', //状态值
		  orderStatusVal: 0, //状态值
		  date: '时间',  //具体时间
		  dates: '',
		  searchPageNum: 1,   // 设置加载的第几次，默认是第一次
		  tradingRecordListData: [],//交易记录列表数据
		  upslideLoading: true,  //"上拉加载"的变量，默认true，隐藏
		  upslideNoData: true	//“没有数据”的变量，默认true，隐藏 
	  })
	  //重新请求
	  this.requestorderlist()
  },
  //订单时间
  bindDateChange: function (e) {
	  this.setData({
		  date: e.detail.value,
		  dates: e.detail.value,
		  searchPageNum: 1,   // 设置加载的第几次，默认是第一次
		  tradingRecordListData: [],//交易记录列表数据
		  upslideLoading: true,  //"上拉加载"的变量，默认true，隐藏
		  upslideNoData: true	//“没有数据”的变量，默认true，隐藏 
	  })
	  //重新请求
	  this.requestorderlist()
  },
  tradingRecordDetailTap:function(e){
	 var Listid = e.currentTarget.dataset.listid;
   var types = e.currentTarget.dataset.type;
	app.publicnavigateTologing('/pages/tradingRecordDetail/tradingRecordDetail?ordernum=' + Listid + '&types=' + types)
  },
  //请求列表
  requestorderlist: function (e) {
	  var that = this;
	  //开启加载
    if (that.data.ifRefresh == false) {
	  that.setData({ upslideLoading: false })
    }
	  //提交给服务端注册信息
	  util.AJAX('Home/Order/OrderList', { uid: app.globalData.userInfoId, orderST: that.data.orderStatusVal, dates: that.data.dates, page: that.data.searchPageNum }, function (res) { //方法体
		  //关闭加载
      if (that.data.ifRefresh == false) {
		  that.setData({ upslideLoading: true })
      }

		  if (res.data.StatusCode == 100) {  //成功
			  var datarow = res.data.info
			  var datalen = res.data.info.length
			  if (datalen) {
				  var newrow = []
				  for (var i = 0; i < datalen; i++) {
					  //快递switch
            if (datarow[i].logistics == 'none'){
              var logisticsstr = ''
            } else if (datarow[i].orderspaystatus == 1){
              var logisticsstr = '待支付'
            }else{
              var logisticsstr = datarow[i].logistics.cordTime + ' ' + datarow[i].logistics.sstatus
            }
            var compleStatus = false
            var buttonStatuses = ''
            var buttonStatusTextes = ''

            if (datarow[i].ordersstatus == 5){ //完成
              compleStatus = true
            }

            if (datarow[i].orderspaystatus == 1){
              buttonStatusTextes = '去支付'
              buttonStatuses = 'BtnRed'
            }else if(datarow[i].ordersopendrstatus == 0) {
              buttonStatusTextes = '打开柜子'
              buttonStatuses = 'BtnSkyblue'
            } else if (datarow[i].ordersstatus == 4) {
              buttonStatusTextes = '确认收货'
              buttonStatuses = 'BtnBlue'
            }else{
              buttonStatusTextes = ''
              buttonStatuses = 'BtnHidden'
            }

					  var lsarray = {
              id: datarow[i].id,
						  orderNum: datarow[i].ordernum,
						  orderStatus: datarow[i].sstatus,
						  orderAddress: datarow[i].address,
						  orderCases: datarow[i].sanum,
						  actualPayment: datarow[i].money,
						  orderTime: datarow[i].createTime,
              types: datarow[i].type,
						  LatestLogistics: logisticsstr,
              completeStatus: compleStatus,   //订单是否完成状态，true为已完成，false为未完成
              buttonStatus: buttonStatuses, //列表按钮状态,"BtnRed"为去支付，"BtnSkyblue"为打开柜子，"BtnBlue"为确认收货，"BtnHidden"为隐藏按钮
              buttonStatusText: buttonStatusTextes,
					  }
					  //加入
					  newrow.push(lsarray)
				  }

				  if (that.data.searchPageNum == 1) {
					  that.setData({ tradingRecordListData: newrow })
				  } else {
					  var tradingRecordListData = that.data.tradingRecordListData
					  var cs = tradingRecordListData.concat(newrow)
					  that.setData({
						  tradingRecordListData: cs,
					  })
				  }
			  }else{
				  //更新显示状态和减掉分页
				  if (that.data.searchPageNum > 1){
					  that.setData({
						  searchPageNum: that.data.searchPageNum - 1,//减掉分页数
						  upslideLoading: true,  //"上拉加载"的变量，默认true，隐藏
						  upslideNoData: false	//“没有数据”的变量，默认true，隐藏 
					  })
				  }
			  }
		  } else {
			  //更新显示状态和减掉分页
			  if (that.data.searchPageNum > 1) {
				  that.setData({
					  searchPageNum: that.data.searchPageNum - 1,//减掉分页数
					  upslideLoading: true,  //"上拉加载"的变量，默认true，隐藏
					  upslideNoData: false	//“没有数据”的变量，默认true，隐藏
				  })
			  }
		  }

      //更新暂无数据显示状态
      if (that.data.tradingRecordListData.length) {
        that.setData({
          unorderlistst: false
        })
      } else {
        that.setData({
          unorderlistst: true
        })
      }

      //判断并关闭下拉刷新
      if (that.data.ifRefresh) {
        that.setData({ ifRefresh: false })
        // complete
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
	  }, 'POST')
  },
  //公共执行方法
  publicfun:function(e){
    var that = this

    if (that.data.buttonclickdou == true){  //开启
      //首先开启加载，并更新点击状态
      that.setData({
        buttonclickdou: false,
        submitload: true, //点击按钮的加载状态
      })

      setTimeout(function () {  //延时操作
        var chgval = e.currentTarget.dataset.chgval
        var orderid = e.currentTarget.dataset.listids
        var ordernum = e.currentTarget.dataset.listnum
        var types = e.currentTarget.dataset.type
        var Info = orderid + '|' + ordernum
        if (chgval == 'BtnRed') {
          //更新状态
          that.setData({
            buttonclickdou: true,
            submitload: false, //点击按钮的加载状态
          })
          if (types == 1){
            wx.redirectTo({
              url: '/pages/pay/pay?ordernum=' + Info
            })
          }else{
            wx.redirectTo({
              url: '/pages/bigpay/bigpay?ordernum=' + Info
            })
          }
        }
        if (chgval == 'BtnSkyblue') {
          util.AJAX('Home/Index/openCabint', { ordernums: Info }, function (res) { //方法体
            //更新状态
            that.setData({
              buttonclickdou: true,
              submitload: false, //点击按钮的加载状态
            })

            if (res.data.StatusCode == 100) { //打开成功
              wx.showModal({
                title: '提示',
                showCancel: false,
                content: res.data.Msg
              })
              //更新列表
              that.requestorderlist()
            } else {
              wx.showModal({
                title: '提示',
                showCancel: false,
                content: '柜子打开失败'
              })
            }
          }, 'POST')
        }
        if (chgval == 'BtnBlue') {
          //远程更新订单状态
          util.AJAX('Home/Index/confirmorder', { ordernums: Info, types:types }, function (res) { //方法体
            //更新状态
            that.setData({
              buttonclickdou: true,
              submitload: false, //点击按钮的加载状态
            })

            if (res.data.StatusCode == 100) { //操作成功
              wx.showToast({
                title: res.data.Msg,
                mask:true,
                icon: 'success',
                duration: 2000
              })
              //更新列表
              that.requestorderlist()
            } else {
              wx.showModal({
                title: '提示',
                showCancel: false,
                content: res.data.Msg
              })
            }
          }, 'POST')

        }
      }, 400)
    }  
  }
})